<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/16
 * Time: 7:05 PM
 */

namespace App\BO\Coupon\CouponProductSkuCheck;

use App\Utils\Tool;
use Illuminate\Support\Facades\Log;

/**
 * 服务时间检查
 */
class ServiceTimeCheck implements ICouponProductSkuCheck
{
    public static function check(CouponProductSkuCheckMeta $checkMeta, \Closure $next)
    {
        $cartItemEntity = $checkMeta->getCartItemEntity();
        $coupon = $checkMeta->getCouponEntity();

        $assignTime = $cartItemEntity->getAssignTime();
        $time = Tool::periodCode2StrNew($assignTime, true);

        // assignDate 只精确到天，所以加上一个秒数
        $orderServiceTime = strtotime($cartItemEntity->getAssignDate() . ' ' . $time);
        //如果优惠券存在 最早可预约时间 和 最晚可预约时间 进行时间检查
        if (($bookNotBefore = $coupon->getBookNotBefore()) && ($orderServiceTime < strtotime($bookNotBefore))) {

            Log::info('优惠券' . $coupon->getId() . '末到可预约时间');
            $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
            $checkResultWrapper->setMessage('末到可预约时间');
            return $checkResultWrapper;

        } elseif (($bookNotAfter = $coupon->getBookNotAfter()) && ($orderServiceTime > strtotime($bookNotAfter))) {

            Log::info('优惠券' . $coupon->getId() . '超过可预约时间');
            $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
            $checkResultWrapper->setMessage('超过可预约时间');
            return $checkResultWrapper;

        }

        return $next($checkMeta);
    }
}
